package org.example.weather.bigscreen.core.service;

import org.apache.commons.collections.CollectionUtils;
import org.example.weather.bigscreen.core.api.dto.AreaDTO;
import org.example.weather.bigscreen.core.entity.TblWeatherArea;
import org.example.weather.bigscreen.core.repository.TblWeatherAreaMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WeatherAreaService {

    @Resource
    private TblWeatherAreaMapper tblWeatherAreaMapper;

    public int insert(TblWeatherArea record) {
        return tblWeatherAreaMapper.insert(record);
    }

    public TblWeatherArea selectByPrimaryKey(String areaId) {
        return tblWeatherAreaMapper.selectByPrimaryKey(areaId);
    }

    public Map<String, Boolean> selectByAreaIds(List<String> areaIds) {
        List<TblWeatherArea> weatherAreas = tblWeatherAreaMapper.selectByAreaIds(areaIds);
        if (CollectionUtils.isNotEmpty(areaIds)) {
            Map<String, Boolean> defaultMap = areaIds.stream().collect(Collectors.toMap(e -> e, e -> false));
            defaultMap.putAll(weatherAreas.stream().collect(Collectors.toMap(TblWeatherArea::getAreaId, TblWeatherArea::getWeatherEnabled)));
            return defaultMap;
        } else {
            return new HashMap<>();
        }
    }

    public int updateByPrimaryKey(TblWeatherArea record) {
        return tblWeatherAreaMapper.updateByPrimaryKey(record);
    }

    public int batchInsert(List<TblWeatherArea> list) {
        return tblWeatherAreaMapper.batchInsert(list);
    }

    public List<AreaDTO> selectAll(Boolean enabled) {

         return tblWeatherAreaMapper.selectAll(enabled);
    }

}
